Declaring final variables

V

vincente13

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

Jürgen Exner

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
 
V

vincente13

"final"

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

DJ Stunks

"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
 
V

vincente13

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

Jürgen Exner

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
 
V

vincente13

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
 
A

Ala Qumsieh

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
 
D

Dave

Jürgen Exner said:
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;
 
M

Mirco Wahab

Thus spoke (e-mail address removed) (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
 
M

Mirco Wahab

Thus spoke (e-mail address removed) (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
 
I

Ingo Menger

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;
 
P

Peter Scott

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;
 
T

Tad McClellan

SteveP said:
JP requests context.

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


If you think that a threaded news reader solves the problems
introduced by not quoting context, then you don't yet
understand the propogation/administration of Usenet articles.

The followup may arrive at someone's news server *before* the
post that it is following up to.

The article that you are following up to may *never* arrive at
someone's news server.

The article that you are following up to may have expired, and
been dropped from someone's news server spool.

Having a threaded newsreader does not help in solving any
of those problems.



And even apart from the vagaries of NNTP, quoting no context
causes problems in the human realm.

For someone participating in 30 threads, what was said in which
thread to too much to keep in (grey matter) memory.



And even apart from the problems that quoting no context introduces
for NNTP and for other humans, it can create a problem for the
posting human himself.

Those 30-thread-at-a-time folks will simply go to the next thread
rather than spend the time to go recapture the context of the thread.
If they happened to know the answer, you won't end up getting it.

Who might these 30-thread-at-a-time folks be? They are very likely
the frequent-answerers, just the people you *want* to pay attention
to your problem!


And apart from all of those, no-quoting can hurt your chances of
getting answers to every _future_ Perl question that you may have...
 
J

Juha Laiho

(e-mail address removed) 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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,188
Messages
2,571,002
Members
47,591
Latest member
WoodrowBut

Latest Threads

Top