Style questions

C

Colin B.

Hey all;

I'm not a programmer, and never have been. I can grind out a decent shell
script, and beat my head against perl until things work, but I'm seldom
happy with the inelegance of the solution. Does anyone have any good style
guides for perl?

And more specifically, I've got the following general case that keeps coming
up routinely.

foreach (`some_unix_command_that_puts_out_multiple_lines`) {
chomp;
split;
(do_stuff_on_the_separate_fields);
}

Anything to fix up there?

Thanks,
Colin
 
T

Tad McClellan

Michele Dondi said:
Personally I have an idiosincrasy with
backticks and prefer qx with alternative delimiters, but that's just ^^^^
me.
^^

No it isn't.

:)
 
T

Ted Zlatanov

And more specifically, I've got the following general case that keeps coming
up routinely.

foreach (`some_unix_command_that_puts_out_multiple_lines`) {
chomp;
split;
(do_stuff_on_the_separate_fields);
}

Anything to fix up there?

I prefer to open "some_unix_command_that_puts_out_multiple_lines|" as
a file and read it line by line. You can catch errors more easily
that way.

Ted
 
C

Colin B.

Michele Dondi said:
There's the one that comes with Perl:

perldoc perlstyle

Cool. Thanks!
Nothing wrong with this. Personally I have an idiosincrasy with
backticks and prefer qx with alternative delimiters, but that's just
me. (That's also in shell scripts, where I prefer $() all the time,
but there the latter has the additional, *technical*, advantage of
allowing nesting.)

While I can appreciate this, I do too much stuff that is necessarily in a
Bourne shell, where $() doesn't work.
Other than that, if your unix command puts out *lots* of multiple
lines, you may want to use a "piped" open() instead, and read line by
line, although all in all it is a more verbose solution.

Duly noted--this is mostly for 'file line quickies at the command line,'
but I'll make a mental note to use open() instead, in significant (or at
least permanent) scripts.
split() in void context is deprecated. Perl emits a warning if
warnings are on. I strongly recommend you put the following two lines
at the beginning of your scripts:

So I'm not entirely sure about this. I've now read the man page and it
says that implicit split() to @_ is deprecated because of the very fact of
clobbering @_. After some thinking, this made good sense and I can see why
I wouldn't want to (generally) do it.

However, is this really void context? I would have thought that it was in
list context, because of the fact that it's dumping to @_. Or is it that
populating @_ is the default behaviour (in this case) for void context?

Actually, what is the definitive definition of void context? If something
dumps its result to @_ or $_ or the like, then is it actually in void
context?

Thanks,
Colin
 

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
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top