Home > Archive > PERL Beginners > May 2006 > Setting variables with eval
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 |
Setting variables with eval
|
|
| Steve Swift 2006-05-07, 7:01 pm |
| I've written a tiny program to make it easy to test the syntax and
effects of a Perl statement. My program is called "perltry" and here it
is in its full gory (pun intended)
#!/usr/bin/perl
use strict;
use warnings;
system('clear');
system('perl -v');
print "Go on - try a few... Enter 'exit' to end.\n";
while (<> ) {
eval $_;
if ($@) {print "$@\n";}
print ' ','.'x50," perltry on $^O\n"
};
If I enter a statement, such as "$a=1;" (without the quotes) then I
would expect that scalar $a would get the value 1. If I then enter the
statement "print $a" I expected to see "1". What I saw was:
Use of uninitialized value in string at (eval 2) line 1, <> line 2.
Could someone tell me why this happened, please?
Is it possible to change this so that the variable $a would get set?
This is my very first post to a Perl newsgroup, so treat me gently, please!
--
Steve Swift (aka "Swifty")
| |
| Bjorge Solli 2006-05-07, 7:01 pm |
| On Friday 05 May 2006 12:29, Steve Swift wrote:
> I've written a tiny program to make it easy to test the syntax and
> effects of a Perl statement. My program is called "perltry" and here it
> is in its full gory (pun intended)
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> system('clear');
> system('perl -v');
> print "Go on - try a few... Enter 'exit' to end.\n";
> while (<> ) {
> eval $_;
> if ($@) {print "$@\n";}
> print ' ','.'x50," perltry on $^O\n"
> };
>
> If I enter a statement, such as "$a=3D1;" (without the quotes) then I
my $a =3D 1;
> would expect that scalar $a would get the value 1. If I then enter the
> statement "print $a" I expected to see "1". What I saw was:
> Use of uninitialized value in string at (eval 2) line 1, <> line 2.
This is because the scope of the variable $a is only within the block. You=
=20
might try using 'our' instead of 'my', but I'm also a beginner so I don't=20
know the meanings of 'our'.
> Could someone tell me why this happened, please?
> Is it possible to change this so that the variable $a would get set?
You might also use perl in oneliner-mode:
perl -w -e '$a=3D1;print "$a\n";'
See also the -n and -p parameters.
> This is my very first post to a Perl newsgroup, so treat me gently, pleas=
e!
Welcome!
=2D-=20
Bj=F8rge Solli - Office:+47 55205847 http://www.nersc.no
Nansen Environmental and Remote Sensing Center - Bergen, Norway
Dept.: Mohn-Sverdrup Center for Global Ocean Studies=20
and Operational Oceanography
| |
| John W. Krahn 2006-05-07, 7:01 pm |
| Steve Swift wrote:
> I've written a tiny program to make it easy to test the syntax and
> effects of a Perl statement. My program is called "perltry" and here it
> is in its full gory (pun intended)
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> system('clear');
> system('perl -v');
> print "Go on - try a few... Enter 'exit' to end.\n";
> while (<> ) {
> eval $_;
> if ($@) {print "$@\n";}
> print ' ','.'x50," perltry on $^O\n"
> };
>
> If I enter a statement, such as "$a=1;" (without the quotes) then I
> would expect that scalar $a would get the value 1. If I then enter the
> statement "print $a" I expected to see "1". What I saw was:
> Use of uninitialized value in string at (eval 2) line 1, <> line 2.
>
> Could someone tell me why this happened, please?
> Is it possible to change this so that the variable $a would get set?
It works fine for me:
$ perl -lne'eval; print $@ if $@'
$a = 1;
print $a;
1
John
--
use Perl;
program
fulfillment
| |
| Mr. Shawn H. Corey 2006-05-07, 7:01 pm |
| On Fri, 2006-05-05 at 11:29 +0100, Steve Swift wrote:
> I've written a tiny program to make it easy to test the syntax and
> effects of a Perl statement. My program is called "perltry" and here it
> is in its full gory (pun intended)
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> system('clear');
> system('perl -v');
> print "Go on - try a few... Enter 'exit' to end.\n";
> while (<> ) {
> eval $_;
> if ($@) {print "$@\n";}
> print ' ','.'x50," perltry on $^O\n"
> };
>
> If I enter a statement, such as "$a=1;" (without the quotes) then I
> would expect that scalar $a would get the value 1. If I then enter the
> statement "print $a" I expected to see "1". What I saw was:
> Use of uninitialized value in string at (eval 2) line 1, <> line 2.
>
> Could someone tell me why this happened, please?
> Is it possible to change this so that the variable $a would get set?
It works on my machine.
You could try my favourite Perl one-liner, the Perl calculator:
perl -ple '$_=eval'
or on DOS:
perl -ple "$_=eval"
See `perldoc perlrun` and `perldoc -f eval` for details.
--
__END__
Just my 0.00000002 million dollars worth,
--- Shawn
"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/
| |
| Paul Lalli 2006-05-07, 7:01 pm |
| Steve Swift wrote:
> I've written a tiny program to make it easy to test the syntax and
> effects of a Perl statement. My program is called "perltry" and here it
> is in its full gory (pun intended)
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> system('clear');
> system('perl -v');
> print "Go on - try a few... Enter 'exit' to end.\n";
> while (<> ) {
> eval $_;
> if ($@) {print "$@\n";}
> print ' ','.'x50," perltry on $^O\n"
> };
>
> If I enter a statement, such as "$a=1;" (without the quotes) then I
> would expect that scalar $a would get the value 1. If I then enter the
> statement "print $a" I expected to see "1". What I saw was:
> Use of uninitialized value in string at (eval 2) line 1, <> line 2.
>
> Could someone tell me why this happened, please?
> Is it possible to change this so that the variable $a would get set?
Your code works fine for me. I suggest that you typo'd when you gave
input:
=============================
This is perl, v5.6.1 built for sun4-solaris
Copyright 1987-2001, Larry Wall
Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5 source
kit.
Complete documentation for Perl, including FAQ lists, should be found
on
this system using `man perl' or `perldoc perl'. If you have access to
the
Internet, point your browser at http://www.perl.com/, the Perl Home
Page.
Go on - try a few... Enter 'exit' to end.
$a=1;
.................................................. perltry on solaris
print $a;
1 .................................................. perltry on
solaris
exit
=============================
Paul Lalli
| |
| Steve Swift 2006-05-07, 7:01 pm |
| > It works fine for me:
But you're cheating and not using my program! :-)
If I comment out the "use warnings" in my program then it works as
expected (at least, as *I* expected it to)
--
Steve Swift (aka "Swifty")
| |
| Chas Owens 2006-05-07, 7:01 pm |
| On 5/5/06, Steve Swift <Steve_Swift@uk.ibm.com> wrote:
> I've written a tiny program to make it easy to test the syntax and
> effects of a Perl statement. My program is called "perltry" and here it
> is in its full gory (pun intended)
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> system('clear');
> system('perl -v');
> print "Go on - try a few... Enter 'exit' to end.\n";
> while (<> ) {
> eval $_;
> if ($@) {print "$@\n";}
> print ' ','.'x50," perltry on $^O\n"
> };
>
> If I enter a statement, such as "$a=3D1;" (without the quotes) then I
> would expect that scalar $a would get the value 1. If I then enter the
> statement "print $a" I expected to see "1". What I saw was:
> Use of uninitialized value in string at (eval 2) line 1, <> line 2.
>
> Could someone tell me why this happened, please?
> Is it possible to change this so that the variable $a would get set?
>
> This is my very first post to a Perl newsgroup, so treat me gently, pleas=
e!
>
> --
> Steve Swift (aka "Swifty")
Odd, it works fine when I run it. Could you post a test case that
always produces the warning message? Also it would be helpful to have
some information on the version of Perl you are using and the OS the
script is running under.
By the way, $a and $b are special varaibles used by the sort function
and therefore do not need to be declared with "my", but it is bad form
to use them for anything but the sort function. For short examples it
is best to use $x, $y, and $z or $i, $j, and $k.
| |
| bellurd@wp.pl 2006-05-08, 6:59 pm |
| >> It works fine for me:
>But you're cheating and not using my program! :-)
>If I comment out the "use warnings" in my program then it works as
>expected (at least, as *I* expected it to)
>--
>Steve Swift (aka "Swifty")
I used your program and it works for me in case:
$a=1
print $a
1
Maybe U could try to run your program from command line as
perl perltry.pl ,but i think somethings wrong with configuration.
----------------------------------------------------
Koncert zespołu TOOL!
24 czerwca w katowickim Spodku!
http://klik.wp.pl/?adr=http%3A%2F%2...ol.html&sid=742
|
|
|
|
|