For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2006 > Working with constants.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Working with constants.
nn

2006-09-09, 3:57 am

I tried to work with constants like this:

use constant lParen => "\(";
use constant rParen => "\)";
use constant text => "this is a text";

and then use it like that:

$str =~ s/$lparen($text)$rparen/<$1>/g;

What am I doing wrong? and how can I do it right?

Marcel

2006-09-09, 3:57 am

Hi Nimro

===============================
#!/usr/bin/perl -w
use strict;

my $line_of_text = "(This is text)";
$line_of_text =~ s/\((This is text)\)/$1/;
print "$line_of_text\n";
===============================

regards,
Marcel

nn wrote:
> I tried to work with constants like this:
>
> use constant lParen => "\(";
> use constant rParen => "\)";
> use constant text => "this is a text";
>
> and then use it like that:
>
> $str =~ s/$lparen($text)$rparen/<$1>/g;
>
> What am I doing wrong? and how can I do it right?


Paul Lalli

2006-09-09, 3:57 am

nn wrote:
> I tried to work with constants like this:
>
> use constant lParen => "\(";


You realize this is the same as "(", right?

> use constant rParen => "\)";


Likewise, this is no different than ")".

> use constant text => "this is a text";
>
> and then use it like that:
>
> $str =~ s/$lparen($text)$rparen/<$1>/g;


What? Where did any of those three variables come from? You declared
three constants:
lParen
rParen
text
but now you tried using three variables:
$lparen
$rparen
$text

The two groups have NOTHING do do with each other (not even if you
corrected the capitalization that I can only assume are typos).

> What am I doing wrong?


See above

> and how can I do it right?


Well for starters, you can tell us what you're *trying* to do, why you
think you need to put a parenthesis in a constant. Because certainly
nothing in your code suggests that.

Also, you can read perldoc constant to learn how to use constants in
double-quote-like contexts, such as regular expressions.

Finally, you can search http://search.cpan.org for the Readonly module,
which is a phenomenally better option to use than the constant pragma.

Paul Lalli

nn

2006-09-09, 6:58 pm

I just want to use constants within a substitute command. the perldoc
doesn't describes using constants with a substitute/matching commands.

thanks


Paul Lalli wrote:
> nn wrote:
>
> You realize this is the same as "(", right?
>
>
> Likewise, this is no different than ")".
>
>
> What? Where did any of those three variables come from? You declared
> three constants:
> lParen
> rParen
> text
> but now you tried using three variables:
> $lparen
> $rparen
> $text
>
> The two groups have NOTHING do do with each other (not even if you
> corrected the capitalization that I can only assume are typos).
>
>
> See above
>
>
> Well for starters, you can tell us what you're *trying* to do, why you
> think you need to put a parenthesis in a constant. Because certainly
> nothing in your code suggests that.
>
> Also, you can read perldoc constant to learn how to use constants in
> double-quote-like contexts, such as regular expressions.
>
> Finally, you can search http://search.cpan.org for the Readonly module,
> which is a phenomenally better option to use than the constant pragma.
>
> Paul Lalli


Paul Lalli

2006-09-09, 6:58 pm

nn wrote:
> I just want to use constants within a substitute command.


You might want to, but you shouldn't want to. You should want to use
the Readonly.pm module.

> the perldoc
> doesn't describes using constants with a substitute/matching commands.


use constant FOO=>'bar';
print "Yes\n" if 'barbell' =~ /@{[FOO]}/;

constants are just subroutines, so in order to use them in
double-quotish contexts, you have to use the same ugly "trick" that you
use to include subroutine return values in a string - put the
subroutine call in an anonymous array reference, and dereference that
reference.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com