arshadmehmood118 has asked for the wisdom of the Perl Monks concerning the following question:

Dear experts, I am beginner in perl. I want a series of files in which I am changing the coordinates of atoms. The file looks like:

$contrl scftyp=rhf RUNTYP=energy coord=unique MULT=1 icharg=0 $end $system memory=8000000 $end $SYSTEM TIMLIM=600 MEMORY=200000 $END $BASIS EXTFIL=.T. GBASIS=full $END $Data H4 Linear RHF/aug-ccpvtz C1 H 1 0.00 0.0 0.00 H 1 0.00 0.0 2.88 H 1 0.00 0.0 5.76 H 1 0.00 0.0 8.64 $END

Mean I want the symbol $ as part of my files. I have written the following perl script:

#/usr/bin/perl use strict; my $head1="$contrl scftyp=rhf RUNTYP=energy coord=unique MULT=1 icharg +=0 $end $system memory=8000000 $end $SYSTEM TIMLIM=600 MEMORY=200000 $END $BASIS EXTFIL=.T. GBASIS=full $END $DATA H4 Linear RHF/aug-ccpvtz C1 H 1 0.00 0.00 0.00"; my $head2="H 1 0.00 0.00 "; my $head3="H 1 0.00 0.00 "; my $head4="H 1 0.00 0.00 "; my $foot="$END"; for(my $R=0.05;$R<=10.50;$R=$R+0.05){ my $filename=sprintf("H4_Linear_RHF_%05.2f",$R); print "Writing bond length $R to file $filename\n"; open(F,">$filename.inp"); my $Rstring=sprintf("%.2f",$R); my $R1string=sprintf("%.2f",$R*2); my $R2string=sprintf("%.2f",$R*3); my $s= "$head1\n $head2 $Rstring\n $head3 $R1string\n $head4 $R2st +ring\n$foot"; print F "$s"; close (F); }

But when I run this script, I am getting the following errors:

Global symbol "$contrl" requires explicit package name at write_H4_RHF +_aug-cc-pvtz.prl line 4. Global symbol "$end" requires explicit package name at write_H4_RHF_au +g-cc-pvtz.prl line 4. Global symbol "$system" requires explicit package name at write_H4_RHF +_aug-cc-pvtz.prl line 4. Global symbol "$end" requires explicit package name at write_H4_RHF_au +g-cc-pvtz.prl line 4. Global symbol "$SYSTEM" requires explicit package name at write_H4_RHF +_aug-cc-pvtz.prl line 4. Global symbol "$END" requires explicit package name at write_H4_RHF_au +g-cc-pvtz.prl line 4. Global symbol "$BASIS" requires explicit package name at write_H4_RHF_ +aug-cc-pvtz.prl line 4. Global symbol "$END" requires explicit package name at write_H4_RHF_au +g-cc-pvtz.prl line 4. Global symbol "$DATA" requires explicit package name at write_H4_RHF_a +ug-cc-pvtz.prl line 4. Global symbol "$END" requires explicit package name at write_H4_RHF_au +g-cc-pvtz.prl line 15. Execution of write_H4_RHF_aug-cc-pvtz.prl aborted due to compilation e +rrors.

Please suggest me any solution that how to make $ symbol as the part of my files generated using this script without errors. I shall be thankful.

Replies are listed 'Best First'.
Re: Help in putting "$" as part of output
by hexcoder (Curate) on Mar 11, 2016 at 08:59 UTC
    Replace

    my $head1="$...";

    with

    my $head1='$...';

    This should work as long as there are no single quotes ' in your string.

    my $head1=q{$...}; works too. For details see Quote and Quote like Operators

Re: Help in putting "$" as part of output
by Discipulus (Canon) on Mar 11, 2016 at 08:23 UTC
    Escape it
    print "\$adollarsign";

    Is surely in the docs but i cannot find it easely.. anyway look at quoted-interpolated-and-escaped-strings

    PS perlop tell us Unary "\" creates a reference to whatever follows it. See perlreftut and perlref. Do not confuse this behavior with the behavior of backslash within a string, although both forms do convey the notion of protecting the next thing from interpolation.

    PPS quotemeta can be also helpfull in your case. You can also use single quote or heredoc to prevent interpolation.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Help in putting "$" as part of output
by Marshall (Canon) on Mar 11, 2016 at 09:16 UTC
    Your code is confusing to me and I am not sure what you are trying to do. However to answer part of the question, consider this:
    #!usr/bin/perl use warnings; use strict; #to print something verbatim, just use a single tick quote, ' versus " my $head1='$contrl scftyp=rhf RUNTYP=energy coord=unique MULT=1 icharg ++=0 $end'; print $head1; __END__ Prints: $contrl scftyp=rhf RUNTYP=energy coord=unique MULT=1 icharg+=0 $end
Re: Help in putting "$" as part of output
by Marshall (Canon) on Mar 13, 2016 at 04:48 UTC
    I took another look at your code. I took out the file opens so that that wouldn't clog up my directory. Here is the only part of the code that is "runnable". Your "for" loop works, but what the stuff above, presumably from the input file means is a mystery.
    #!usr/bin/perl use warnings; use strict; # I have no idea what the input file (here __DATA__) is supposed # to mean and be used? print "*** no idea what this stuff means***\n"; while (<DATA>) ##I just print this stuff.... { print; } print "********\n"; print "\n\n"; my $head1='$contrl scftyp=rhf RUNTYP=energy coord=unique MULT=1 icharg +=0 $end'; my $head2="H 1 0.00 0.00 "; my $head3="H 1 0.00 0.00 "; my $head4="H 1 0.00 0.00 "; my $foot='$END'."\n"; for(my $R=0.05;$R<=10.50;$R=$R+0.05) { my $filename=sprintf("H4_Linear_RHF_%05.2f",$R); printf ("%s %05.2f %s", "Writing bond length", $R, "to file $file +name\n"); my $Rstring=sprintf("%.2f",$R); my $R1string=sprintf("%.2f",$R*2); my $R2string=sprintf("%.2f",$R*3); my $s= "$head1\n $head2 $Rstring\n $head3 $R1string\n $head4 $R2st +ring\n$foot"; print "$s"; } =EXAMPLE PRINTOUT.... This uses the POD (Plain Old Documentation) tags to "cheat" and add an example printout to the code. Of course this has to be before the __DATA__ segment. If there was no __DATA__ segment, then a simple "__END__" would work. *** no idea what this stuff means*** $contrl scftyp=rhf RUNTYP=energy coord=unique MULT=1 icharg=0 $end $system memory=8000000 $end $SYSTEM TIMLIM=600 MEMORY=200000 $END $BASIS EXTFIL=.T. GBASIS=full $END $Data H4 Linear RHF/aug-ccpvtz C1 H 1 0.00 0.0 0.00 H 1 0.00 0.0 2.88 H 1 0.00 0.0 5.76 H 1 0.00 0.0 8.64 $END ******** Writing bond length 00.05 to file H4_Linear_RHF_00.05 $contrl scftyp=rhf RUNTYP=energy coord=unique MULT=1 icharg=0 $end H 1 0.00 0.00 0.05 H 1 0.00 0.00 0.10 H 1 0.00 0.00 0.15 $END Writing bond length 00.10 to file H4_Linear_RHF_00.10 $contrl scftyp=rhf RUNTYP=energy coord=unique MULT=1 icharg=0 $end H 1 0.00 0.00 0.10 H 1 0.00 0.00 0.20 H 1 0.00 0.00 0.30 $END Writing bond length 00.15 to file H4_Linear_RHF_00.15 $contrl scftyp=rhf RUNTYP=energy coord=unique MULT=1 icharg=0 $end H 1 0.00 0.00 0.15 H 1 0.00 0.00 0.30 H 1 0.00 0.00 0.45 $END Writing bond length 00.20 to file H4_Linear_RHF_00.20 $contrl scftyp=rhf RUNTYP=energy coord=unique MULT=1 icharg=0 $end H 1 0.00 0.00 0.20 H 1 0.00 0.00 0.40 H 1 0.00 0.00 0.60 $END Writing bond length 00.25 to file H4_Linear_RHF_00.25 $contrl scftyp=rhf RUNTYP=energy coord=unique MULT=1 icharg=0 $end H 1 0.00 0.00 0.25 H 1 0.00 0.00 0.50 H 1 0.00 0.00 0.75 $END ... many lines deleted .... =cut __DATA__ $contrl scftyp=rhf RUNTYP=energy coord=unique MULT=1 icharg=0 $end $system memory=8000000 $end $SYSTEM TIMLIM=600 MEMORY=200000 $END $BASIS EXTFIL=.T. GBASIS=full $END $Data H4 Linear RHF/aug-ccpvtz C1 H 1 0.00 0.0 0.00 H 1 0.00 0.0 2.88 H 1 0.00 0.0 5.76 H 1 0.00 0.0 8.64 $END
Re: Help in putting "$" as part of output
by perlfan (Parson) on Mar 12, 2016 at 00:38 UTC
    Please change:

    open(F,">$filename.inp");

    to

    open my $F, ">", "$filename.inp";

    Then change all subsequent references from F to $F.

    See open for more details. That won't fix your current issue, but it'll level up your code and beginner status.

      See open for more details. That won't fix your current issue, but it'll level up your code and beginner status.

      use autodie;

        I was trying to be semantically equivalent to the OP. Sure, I could have said open(F,">$filename.inp") or die;, but my intent was not to advocate for an error handling strategy one way or another. I've also never had a need to use autodie, so it's not a go-to of mine. I know people who swear by it and those who hate it. Me, I am indifferent. And lazy.