Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Concatnate long statement

by phimtau123 (Novice)
on Jul 03, 2007 at 21:10 UTC ( #624788=perlquestion: print w/ replies, xml ) Need Help??
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)=@_;

Comment on Concatnate long statement
Download Code
Re: Concatnate long statement
by Util (Priest) on Jul 03, 2007 at 21:26 UTC

    Do you mean something like this?

    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.

      so we dont need any special operator? like in vb we use the & for long sql statement. Thank

        You are confusing two concepts.

        To concatenate strings is to combine two strings into one. The string concatenation operator is & in VB and . in Perl.

        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 ";
        Welcome to PerlMonks - I think you will find your life more enjoyable here than with VB :)

        Certainly free form code is much nicer. For instance you can use the string concatenation or newline examples given in other answers for tidying up your code in Socket Problem.

        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.
Re: Concatnate long statement
by FunkyMonk (Canon) 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.

      And if not refactor, at least switch to using pseudo-named arguments by expecting key/value pairs and appending those to defaults.

      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, );
        A variation:
        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 }
Re: Concatnate long statement
by snopal (Pilgrim) on Jul 04, 2007 at 00:39 UTC
    I prefer:
    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.

      my %args = shift;

      ?

        Ah yes, my freeform entry technique sometimes fails me:

        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.
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!

      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? =

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-10-23 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls