ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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";"
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.