LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2010, 10:39 PM   #16
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454

Quote:
Originally Posted by grail View Post
Cheers ... very concise I will try to remember
Now, sometimes you do need to change something in the consumer lexical space. So,
Code:
eval $string;
exist for this reason too .
 
Old 07-21-2010, 10:44 PM   #17
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Example for the above:

Code:
sergei@amdam2:~/junk> cat -n junk.pl
     1  #!/usr/bin/perl -w
     2
     3  use strict;
     4  use warnings;
     5
     6  my $x = 1;
     7
     8  eval '{$x = 2;}';
     9
    10  warn "\$x=$x";
sergei@amdam2:~/junk> ./junk.pl
$x=2 at ./junk.pl line 10.
sergei@amdam2:~/junk>
...

And this is essentially the "C"-like '#include "some_file" recipe.

Last edited by Sergei Steshenko; 07-21-2010 at 10:46 PM.
 
Old 07-22-2010, 06:59 AM   #18
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I didn't know that eval() changed existing variables in your program.

And BTW why are you using both "use warnings;" and "perl -w"?
 
Old 07-22-2010, 08:41 AM   #19
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
And BTW why are you using both "use warnings;" and "perl -w"?
My understanding to this would be that '-w' will deliver the runtime warnings, but the "use warnings" allows you to then create your own,
ie like "warn "\$x=$x";"
 
Old 07-22-2010, 11:17 AM   #20
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by grail View Post
My understanding to this would be that '-w' will deliver the runtime warnings, but the "use warnings" allows you to then create your own,
ie like "warn "\$x=$x";"
I don't think so. I also wondered why Sergei is using both the flag (-w) and the pragma (use warnings;), so we'll see what he says. (The pragma is generally recommended now. For some of the details about how -w and use warnings; differ, see this thread on Stack Overflow.)

In the script, warn is not a warning Sergei created. It's a function built into Perl itself. The difference between warn and print or say is that warn defaults to STDERR, while the others default to STDOUT. I've noticed over the years that Sergei pretty much always uses warn not print for little example scripts like this. I don't know why and haven't seen anyone else do it. It's seems to be a personal preference.

Last edited by Telemachos; 07-22-2010 at 11:21 AM.
 
Old 07-22-2010, 11:26 AM   #21
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
warn also seems to print the line number, which I find annoying. I guess it's good for debugging, though.

To just print to stderr in normal cases, I like:
Code:
print STDERR "this is some text\n"
.
 
Old 07-22-2010, 11:27 AM   #22
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by MTK358 View Post
I didn't know that eval() changed existing variables in your program.
There are two uses of eval. The block form is for exception/error handling. The form Sergei has here, the string form, doesn't exactly "change existing variables." It takes a string and treats it like Perl code. As the docs put it, "the return value of [the expression] is parsed and executed as if it were a little Perl program." It's worth noting that you should not use the string form on any potentially insecure strings (like say, user input). If you ask the user for some string and then eval it and the user puts system("rm *") - as a joke, then you have a problem.
 
Old 07-22-2010, 11:31 AM   #23
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by MTK358 View Post
warn also seems to print the line number, which I find annoying. I guess it's good for debugging, though.

To just print to stderr in normal cases, I like:
Code:
print STDERR "this is some text\n"
.
You can turn off the line-number printing by adding a final new line (as with die).

Code:
#!/usr/bin/env perl
use strict;
use warnings;

warn "What happened?";  # prints a line number
warn "Nothing much.\n"; # no line number
 
Old 07-22-2010, 11:34 AM   #24
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
OK, but what is preferred to print error and warning messages: "warn" or "print STDERR"?
 
Old 07-22-2010, 11:43 AM   #25
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by MTK358 View Post
OK, but what is preferred to print error and warning messages: "warn" or "print STDERR"?
For actual warning or error messages, I would use warn. Sergei also likes it for examples and debugging, that's all. There's no right or wrong here, just different preferences. As for "what is preferred", Perl's motto is "there's more than one way to do it", so if you ask five Perl programmers, you will probably get six opinions.
 
Old 07-22-2010, 11:59 AM   #26
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Hey Telemachos - thanks for the correction and further details ... like i said earlier .. still learning
 
Old 07-22-2010, 01:41 PM   #27
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
...
And BTW why are you using both "use warnings;" and "perl -w"?
Regarding '-w' - automatic habit since the nineties when there was no "use warnings;".
 
Old 07-22-2010, 01:42 PM   #28
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
OK, but what is preferred to print error and warning messages: "warn" or "print STDERR"?

perldoc -f warn

- the very first sentence with explanations.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Recursive difficulty in Perl Robhogg Programming 8 05-28-2010 12:45 PM
Problems with anonymous login proftp 1.3.1: 530-Unable to set anonymous priviliges. gentooox Linux - Server 3 05-03-2009 02:46 PM
Got to be a better way. Perl recursive directory listing (not files) PB0711 Programming 8 03-16-2009 05:24 PM
Perl file handler in recursive function enemorales Programming 4 02-02-2009 03:20 PM
dereferencing anonymous arrays in Perl coolman0stress Programming 6 10-27-2003 10:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration