Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: RFC: A Primer on Writing Portable Perl Programs

by Anonymous Monk
on Nov 01, 2006 at 10:06 UTC ( #581644=note: print w/ replies, xml ) Need Help??


in reply to RFC: A Primer on Writing Portable Perl Programs

In trivial cases like this one, where either type of quotes will work around the Perl string, simply swapping the internal double quotes with the external single quotes can fix the problem. That's because double quotes, unlike single quotes, are recognized as quoting characters by Windows, permitting this reworked command to work as intended:
perl -wl -e "print 'Crikey, what a little beauty!';"
First of all, which kind of quotes you should use isn't a matter of the OS. It's a matter of the shell you are using. And there are good reasons to use single quotes in Unix shells. Because, even in trivial cases as your example, using double quotes won't work as you may expect. See, Perl borrowed a lot from Unix and the shell. A whole lot. Including the difference between single and double quotes. Running your example in bash, a not uncommon Unix shell, gives:
$ perl -wl -e "print 'Crikey, what a little beauty!';" -bash: !': event not found
And in csh:
$ perl -wl -e "print 'Crikey, what a little beauty!';" ';": Event not found.
And in zsh:
$ perl -wl -e "print 'Crikey, what a little beauty!';" zsh: no such event: 0
For this example, using double quotes works fine in the Bourne shell, ash, ksh, and tcsh. Hence, the use of double quotes instead of single quotes isn't portable between shells on a Unix system - good enought reason in itself to use double quotes. Had the example contained an actual scalar variable, the use of double quotes in any Unix shell would have caused a problem: just like in Perl, Unix shells interpolate inside double quotes, and don't interpolate inside single quotes.

So, given 8 shells, sh, ash, bash, ksh, zsh, csh, tcsh and the standard Windows shell, use of single quotes make my one-liner work on 7 of them. Use of double quotes is going to fail on several of them if the command line contains an exclaimation mark, and is going to fail on 7 of them if it contains a scalar variable. Now, if my goal was to maximize portability, I'd pick the solution that works 7 out of 8 times instead of 1 out of 8 times.

And guess what? Various shells, including bash, have been ported to different operating systems, including Windows. And having a Bourne like shell is a requirement for POSIX compliance anyway.


Comment on Re: RFC: A Primer on Writing Portable Perl Programs
Select or Download Code
Re^2: RFC: A Primer on Writing Portable Perl Programs
by Corion (Pope) on Nov 01, 2006 at 10:08 UTC

    Of course, portabit over 7 platforms is only half of the story, as the eighth platform accounts for about 90% of all deployed machines ;)

    (and yes, that number is pulled out of the air and just exists to illustrate the different goals of portability)

    Update: McDarren spotted a typo

      Yes, but of the 90% of all deployed machines, over 99.99% will never have a user issueing Perl one-liners.

      To do statistics in a meaningful way, the number of deployed machines with a certain shell isn't relevant. You'd have to look at the number of users (where a single person working on N platforms counts as N different users) that issue Perl one-liners. And I'm pretty sure that more than 10% of them run non-Windows shells.

      Not to mention that Unix shells have been ported to Windows, and I do know people using Unix shells on Windows (myself included). I've never heard of the Windows shell having been ported to Unix, never mind any one actually running the Windows shell on Unix.

      Oh, and MacOS has gone Unixy as well, so that's another platform that will have a Bourne shell compatible shell available.

      As for VMS and other exotic OSses, my knowledge about them is too limited to comment.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://581644]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (14)
As of 2014-09-30 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (376 votes), past polls