phimtau123 has asked for the wisdom of the Perl Monks concerning the following question:
hi how do you concatenate long statement in perl? for example my long assignment code.
my ($eqpid, $chamber, $lotid, $wafer_flow, $recipe, $wafer_slot, $step
+_value, $alarmtype, $sensor, $signal_level, $alarm_upper_limit, $alar
+m_lower_limit, $code, $date_submited, $time_submited, $filenames,$use
+rcode, $sock)=@_;
Re: Concatnate long statement
by FunkyMonk (Chancellor) on Jul 03, 2007 at 22:10 UTC
|
You really don't want a subroutine (it looks like that's what you're doing) with 18 arguments. It's going to cause you all sorts of problems when when you miss one of them out, or get some of them in the wrong order. That number of arguments really gives off a bad code smell.
Refactor your code now, before it's too late.
| [reply] [Watch: Dir/Any] [d/l] |
|
sub womble {
my @defaults = (
fleem => 12.3,
alarm_low => 99,
alarm_hi => 'naked ernest borgnine',
alarm_hello => 'nice to see you',
alarm_good_evening => STOP_STEALING_PYTHON_GAGS,
quux => 19.95,
);
my %args = ( @defaults, @_ );
if( $args{ quux } > 15 ) { enable_flux_capacitor(); }
## ... rest of womblification routine
}
womble( quux => 1.21 * GIGAWATS_PER_MICROFLEEM,
alarm_low => 42, );
| [reply] [Watch: Dir/Any] [d/l] |
|
use strict;
use warnings;
use Data::Dumper;
my %WOMBLE_DEFAULTS
= (
fleem => 12.3,
alarm_low => 99,
alarm_hi => 'naked ernest borgnine',
alarm_hello => 'nice to see you',
alarm_good_evening => 'STOP_STEALING_PYTHON_GAGS',
quux => 19.95,
);
my $DEBUG = 1;
womble({
quux => 1.21,
alarm_low => 42,
});
exit( 0 );
sub womble {
my ($args_href) = @_;
%$args_href = (%WOMBLE_DEFAULTS, %$args_href);
warn Dumper( $args_href ) if $DEBUG;
## ... rest of womblification routine
}
| [reply] [Watch: Dir/Any] [d/l] |
Re: Concatnate long statement
by Util (Priest) on Jul 03, 2007 at 21:26 UTC
|
my (
$eqpid, # This
$chamber, # looks
$lotid, # like
$wafer_flow, # a
$recipe, # good
$wafer_slot, # place
$step_value, # for
$alarmtype, # some
$sensor, # Comments!
$signal_level,
$alarm_upper_limit, # (C) 1980 Stick-Ups Corporation
$alarm_lower_limit,
$code,
$date_submited,
$time_submited,
$filenames,
$usercode,
$sock,
) = @_;
You should be able to put a newline anywhere you could use a space.
| [reply] [Watch: Dir/Any] [d/l] |
|
so we dont need any special operator? like in vb we use the & for long sql statement. Thank
| [reply] [Watch: Dir/Any] |
|
VB:
var1 = "abc" & var2
Perl (Concatenation):
$var1 = 'abc' . $var2
Perl (Interpolation):
$var1 = "abc$var2";
Perl (Concatenation using join):
$var1 = join('', 'abc', $var2);
In VB, all statements must be on the same line. The _ operator allows you to break a statement into multiple lines. This has nothing to do with string concatenation. Perl doesn't require a continuation operator. Instead, one must tell Perl where the statement ends using ;.
VB:
var = func(var1, var2, var3)
Perl:
$var = func($var1, $var2, $var3);
VB:
var = func( _
var1, _
var2, _
var3 _
)
Perl:
$var = func(
$var1,
$var2,
$var3
);
Together:
VB:
stmt = "SELECT *" & _
" FROM table" & _
" ORDER BY field"
Perl:
$stmt = "SELECT *"
. " FROM table"
. " ORDER BY field"
Perl (Since SQL servers treats newlines as whitespace):
$stmt = "
SELECT *
FROM table
ORDER BY field
";
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
|
The following example has a statement per line.
#!/usr/bin/perl -w
use strict;
use warnings;
use Time::Format 'time_format';
printf "Hello world, today is %s\n", time_format('mm dd yyyy',time);
exit;
The following example has statements broken up into lines and is still valid:
#!/usr/bin/perl -w
use strict;
use warnings;
use Time::Format
'time_format';
printf "Hello world, today is %s\n",
time_format(
'mm dd yyyy', time
);
exit;
The following example is still valid but has unnacceptable formatting for human beings:
#!/usr/bin/perl -w
use
strict;
use
warnings;
use
Time::Format
'time_format';
printf
"Hello world, today is %s\n",
time_format(
'mm dd yyyy',
time)
;
exit
;
The following example breaks:
#!/usr/bin/perl -w
use strict;
use warnings;
use Time::Format 'time_format'
printf "Hello world, today is %s\n", time_format('mm dd yyyy',time);
exit;
Personally I use vim, set expandtab, and I break a long statement with new line and tab.
If you foresee coding perl regularly, you need a copy of Perl Best Practices by Damian Conway.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Concatnate long statement
by naikonta (Curate) on Jul 04, 2007 at 00:51 UTC
|
Perl is a free-form language when comes to syntax layout. You don't need special operator to break your statement into as many lines as you like, or as your eyes can handle. But I'm not going to write a subroutine with that many arguments. I either use hash (for using named parameters instead of positional parameters), or break up the subroutine.
The same applies with string. Just type in the long string, and break it at your will.
my $str = "This string is not that long
but I break it anyway becauase
Perl allows me to do so and I just want to
do it this way. But remember, there will be
newlines inserted between
lines";
Or, if you like, use the concatenation operator:
my $str = "This string is not that long" .
" but I break it anyway becauase" .
" Perl allows me to do so and I just want to" .
" do it this way. But remember, there will not" .
" be newlines inserted between lines" .
" unless I say it explicitly," .
" like\n" .
" this";
Many Perl programmers, however, prefer to use here-doc syntax:
my $str = <<EOF;
This string is not that long
but I break it anyway becauase
Perl allows me to do so and I just want to
do it this way. But remember, there will be
newlines inserted between
lines. I can also put $some ($variables) here.
EOF
Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
As here documents do introduce newlines in your string everywhere you start a new line they are not the same as a simple concatenation, behold:
#!/perl
my $foo = <<EOF;
Would these two lines
become one in a here document?
EOF
print "=$foo=\n";
will output:
=Would these two lines
become one in a here document?
=
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Concatnate long statement
by snopal (Pilgrim) on Jul 04, 2007 at 00:39 UTC
|
my %args = (
eqpid => $eqpid,
chamber => $chamber,
lotid => $lotid,
wafer_flow => $wafer_flow,
recipe => $recipe,
wafer_slot => wafer_slot,
# etc.
);
sub_call(%args);
sub sub_call {
my %args = shift;
# do some validation
# do some work
}
The advantage is that no one (including you) who comes in later has to remember parameter order in the call. The actual structure is sitting right there in the open, and the code is much more self-documenting.
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
sub_call(\%args);
sub sub_call {
my $arg_of = shift;
}
Using a HASHREF makes so much more sense as well.
Thanks for the nice catch.
| [reply] [Watch: Dir/Any] [d/l] |
|
|