For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2006 > Declaring final variables









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 Declaring final variables
vincente13@gmail.com

2006-05-28, 10:01 pm

Hi all,

In Java there is this keyword final that declares the variable to be
final.

e.g. final int number = 0;

How do i do this in Perl?

Thanks

Jürgen Exner

2006-05-28, 10:01 pm

vincente13@gmail.com wrote:
> Hi all,
>
> In Java there is this keyword final that declares the variable to be
> final.
>
> e.g. final int number = 0;
>
> How do i do this in Perl?


Well, what is the semantic of "final"?

jue


vincente13@gmail.com

2006-05-29, 4:07 am

"final"

no other parts of the program is allowed to change the value of the
variable?

DJ Stunks

2006-05-29, 4:07 am

vincent...@gmail.com wrote:
> "final"
>
> no other parts of the program is allowed to change the value of the
> variable?


please quote appropriate context with your replies!

to create a read-only variable:

use Readonly;
Readonly my $javaesque_final_readonly_variable = 76;

HTH,
-jp

vincente13@gmail.com

2006-05-29, 4:07 am

it seems like i dont have the module Readonly....any other workaround?

by the way im trying to create a static variable also..
i've read the manpage on creating a static variable..but i still dont
quite get it..

For e.g.
my $var;
my $string = "This is ".$var." my path";
$var="overwrite";
print $string;

if $var is static, then $string should print out "This is overwrite my
path"

How do i achieve this?

Appreciate any help.

Jürgen Exner

2006-05-29, 4:07 am

vincente13@gmail.com wrote:
> it seems like i dont have the module Readonly.


The why don't you install it?

....any other workaround?

For what? Please quote appropriate context -as has been customary for 2
decades- such that people have a chance to know what you are talking about.

> by the way im trying to create a static variable also..
> i've read the manpage on creating a static variable..but i still dont
> quite get it..
>
> For e.g.
> my $var;
> my $string = "This is ".$var." my path";
> $var="overwrite";
> print $string;
>
> if $var is static, then $string should print out "This is overwrite my
> path"


Why? It should print an error message (or at least a warning) in line 2
because $var is undefined in the concatenation.

You seem to have an insteresting concept of static.

> How do i achieve this?



If you want a deferred evaluation then assign the string with the variable
name (without interpolating the var!) to $string and then eval() it later.

jue


vincente13@gmail.com

2006-05-29, 4:07 am

> For what? Please quote appropriate context -as has been customary for 2
> decades- such that people have a chance to know what you are talking about.


I apologize for not quoting the message. I using googles groups to post
my messages and there isn't any options for me to quote the previous
message?

> You seem to have an insteresting concept of static.


Actually my problem is having a fixed query string, however the
parameters would change accordingly to the user input.

> If you want a deferred evaluation then assign the string with the variable
> name (without interpolating the var!) to $string and then eval() it later.


i don't quite understand this, perhaps you can show a simple e.g.?

Appreciate

Ala Qumsieh

2006-05-29, 4:07 am

vincente13@gmail.com wrote:
> Hi all,
>
> In Java there is this keyword final that declares the variable to be
> final.
>
> e.g. final int number = 0;
>
> How do i do this in Perl?


Not exactly equivalent, but you can use constants instead:

use constant number => 0;

it is a good idea to name your constants in all capitals:

use constant NUMBER => 0;

--Ala

Dave

2006-05-29, 4:07 am


"Jürgen Exner" <jurgenex@hotmail.com> wrote in message
news:8hweg.9972$U_2.4402@trnddc05...
> vincente13@gmail.com wrote:
>
> The why don't you install it?
>
> ...any other workaround?
>
> For what? Please quote appropriate context -as has been customary for 2
> decades- such that people have a chance to know what you are talking
> about.
>
>
> Why? It should print an error message (or at least a warning) in line 2
> because $var is undefined in the concatenation.
>
> You seem to have an insteresting concept of static.
>
>
>
> If you want a deferred evaluation then assign the string with the variable
> name (without interpolating the var!) to $string and then eval() it later.
>
> jue
>

i.e. use an evaluated substitution:

use strict;
use warnings;


my $string = 'This is $var my path';
my $var = "overwrite";

(my $interpolated_string = $string) =~ s/(\$\w+)/$1/eeg;

print $interpolated_string;







Mirco Wahab

2006-05-29, 4:07 am

Thus spoke vincente13@gmail.com (on 2006-05-29 08:21):

> Actually my problem is having a fixed query string, however the
> parameters would change accordingly to the user input.


Just a question, do you mean something like this:

...
my @user_input = qw(overwrite underwrite behindwrite);

for my $var (@user_input) {
my $query_string = qr/This is $var my path/;
do_query( $query_string );
}

sub do_query {
my ($actual_query) = @_;
print $actual_query, "\n";
# search for string in
# some larger text here
}
...

Just guessed this from your
response, I could be wrong here.

So 'static' means just 'use it where you need it'

What exactly are you trying to do?

Regards

Mirco
Mirco Wahab

2006-05-29, 4:07 am

Thus spoke vincente13@gmail.com (on 2006-05-29 07:13):

> For e.g.
> my $var;
> my $string = "This is ".$var." my path";
> $var="overwrite";
> print $string;
>
> if $var is static, then $string should print out "This is overwrite my
> path"
>
> How do i achieve this?


This is *surely* not what
you really want, but here we go ...

my $var;
my $string = "This is ".$var." my path";

BEGIN { $var = "overwrite" }
print $string;

prints: "This is overwrite my path"

Regards

Mirco
Ingo Menger

2006-05-29, 4:07 am


vincente13@gmail.com wrote:
> it seems like i dont have the module Readonly....any other workaround?
>
> by the way im trying to create a static variable also..
> i've read the manpage on creating a static variable..but i still dont
> quite get it..
>
> For e.g.
> my $var;
> my $string = "This is ".$var." my path";
> $var="overwrite";
> print $string;
>
> if $var is static, then $string should print out "This is overwrite my
> path"


This has nothing whatsoever to do with static variables.

>
> How do i achieve this?


You want a subroutine (i.e. in java speak a "static method"):

sub qstring($) {
my $var = shift;
return "This is $var my path.";
}

my $string = qstring "overwrite";
print $string;


>
> Appreciate any help.


Peter Scott

2006-05-29, 7:03 pm

On Sun, 28 May 2006 23:21:26 -0700, vincente13 wrote:
>
> i don't quite understand this, perhaps you can show a simple e.g.?


Something like this:

$s = 'print $x here'; # Single quotes
# Later:
$x = "foo";
$s = eval qq("$s");
print $s;

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

SteveP

2006-05-29, 7:03 pm

JP requests context.

JP, Why don't you use a threaded news reader?

Juha Laiho

2006-05-29, 7:03 pm

vincente13@gmail.com said:
>by the way im trying to create a static variable also..
>i've read the manpage on creating a static variable..but i still dont
>quite get it..
>
>For e.g.
>my $var;
>my $string = "This is ".$var." my path";
>$var="overwrite";
>print $string;
>
>if $var is static, then $string should print out "This is overwrite my
>path"


Well, one solution could be (depending on scoping issues):
#! /usr/bin/perl -w
use strict;

my $x;
sub text {
'Foo '.$x." bar.\n";
}

$x = 'first';
print text;

$x = 'second';
print text;


.... or, with proper scoping and parameter passing:
#! /usr/bin/perl -w
use strict;

sub text {
my $fillin = shift;
'Foo '.$fillin." bar.\n";
}

my $x;
$x = 'first';
print text($x);

$x = 'second';
print text($x);


--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Sponsored Links







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

Copyright 2008 codecomments.com