Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

how to make this perl code run

by ghosh123 (Monk)
on May 16, 2013 at 13:51 UTC ( #1033832=perlquestion: print w/ replies, xml ) Need Help??
ghosh123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi
I have this following perl code of 5 lines. How can I make it run (or compile) without modifying or doing any correction in the first 5 lines.
I will not put the strings in "" or won't use any pragma in the first 5 lines to make it run. Can anything be done from line 6 to make it run.
Is that possible ?

#!/usr/bin/perl -w use strict; my $str1 = hi; my $str2 = hello; my $str3 = hola;

Comment on how to make this perl code run
Download Code
Re: how to make this perl code run
by Random_Walk (Parson) on May 16, 2013 at 13:58 UTC

    Quote your strings

    my $str1 = "Hi"; my $str2 = "Hello and a newline\n";

    Update

    Doh, I just saw, "how do I make this code run". and thought, fix it! I did not read all the constraints.

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

      They said:

      'I will not put the strings in ""'

Re: how to make this perl code run
by demerphq (Chancellor) on May 16, 2013 at 14:01 UTC

    No.

    ---
    $world=~s/war/peace/g

      Although if you were willing to change hi; to hi(); this is easy.

      ---
      $world=~s/war/peace/g

Re: how to make this perl code run
by choroba (Abbot) on May 16, 2013 at 14:12 UTC
    I can make it compile and run without touching the programme at all:
    echo 1 > strict.pm perl -I. 1.pl
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Well I consider any command line tricks cheating, cause otherwise you could also start calling a recompiled Perl.

      Did you try putting the logic in a BEGIN block... (don't know if it does the trick).

      But why did you call the OP's script 1.pl? Doesn't make it easier to understand what you are doing...

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      PS: I'd try a source-filter approach! =)

        I tried putting the logic into a BEGIN block, but without success. It is perhaps too late on line 6.

        When testing code samples, I name them 1.pl, 2.pl, 3.pl and so on. The next day, I remove all of them and start again from 1. It is shorter than using the node's id in the name, but still lets me keep several answers ready for improvement if there are objections, which simple junk.pl would not do :-)

        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: how to make this perl code run (stupid interview questions)
by Anonymous Monk on May 16, 2013 at 14:15 UTC

    stupid interview questions are stupid

      Do they get this questions per email and if they answer well they are invited for a personal encounter?

      And the interviewers don't try to check the answers with google, to see if they maybe appeared at SO or PM?

      I have problems understanding this concept of "interview" ...

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      update

      Or do they ask in the middle of the interview to go to toilet and then start fiddling with their smart phone?

Re: how to make this perl code run
by blue_cowdawg (Monsignor) on May 16, 2013 at 14:28 UTC

    Your question doesn't make a whole lot of sense. If you quote your strings properly the program should run as is. Won't do anything, but it will run. Unless I'm missing something in those six lines...


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: how to make this perl code run
by hdb (Prior) on May 16, 2013 at 14:32 UTC

    Violating the spirit but not the letter of your challenge:

    #!/usr/bin/perl -w use strict; my $str1 = hi(); my $str2 = hello(); my $str3 = hola(); sub hi { "hi" } sub hello { "hello" } sub hola { "hola" } print "$str1,$str2,$str3\n";

      That violates the letter of the challenge; you modified something within the first five lines (added parens).


      Dave

Re: how to make this perl code run
by kennethk (Monsignor) on May 16, 2013 at 14:42 UTC

    Sure, no problem. I'll give you the hint that it involves Exporter and some command-line flags. So is this homework or an interview?

    Update: Well, since moritz put in a great solution, mine is


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Your solution is similar to mine, in fact I started with a similar code, but then I reduced it to a point where Exporter is not needed.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        I would argue that your code modifies the first 5 lines, whereas mine actually executes them.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: how to make this perl code run
by moritz (Cardinal) on May 16, 2013 at 14:56 UTC
Re: how to make this perl code run
by LanX (Canon) on May 16, 2013 at 16:46 UTC
    > (without mucking with strict.pm or adding command line options or using non-standard perl versons).

    and actually really executing the code! =)

    #!/usr/bin/perl -w use strict; my $str1 = hi; my $str2 = hello; my $str3 = hola; print uc("$str1, $str2, $str3 you ... ! ;-)"); BEGIN { open my $file,"<",$0; my $code=do{local $/=undef;<$file>}; $code =~ s/use strict;//; $code =~ s/^BEGIN.*\E//sm; no strict "subs"; no warnings; eval "$code" or die $@; exit; } #EOF

    prints HI, HELLO, HOLA YOU ... ! ;-)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    update

    changed output to less sarcasm

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-12-27 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls