Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Creating a file by using perl

by freekngeek (Acolyte)
on Mar 28, 2013 at 10:46 UTC ( #1025935=perlquestion: print w/replies, xml ) Need Help??
freekngeek has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, I am new to perl and here is my problem. I have two files. First one is perl script which is reading a text file and parsing some important values and putting into an array for future use. Second file is which is a Cadence framework file that do some random stuff, I need values from perl script and want to use those values in my So I was doing this. I was trying to create a new file( and printing out all the lines and commands into this file. For an example I need these lines in my new file:

libName = "cmos28shp" ; device library name tech_lib = "cmos28shp" techFileID = techOpenTechFile( "cmos28shp" "tech.db" "r") lib = ddGetObj(libName) ;; Verify the library exista +nce if(lib == nil then fprintf(stderr "ERROR: Invalid library. %L .\n" libName) return(nil) else
So I was doing this in perl:
my $outfile = ""; open FH, ">> $outfile"; print FH "libName = "cmos28shp"\n"; print FH 'tech_lib = "cmos28shp"'; close FH;
I am not sure if it's a right way to generate this file, I am also getting errors like these :
Bareword found where operator expected at line 12, near "" +libName = "cmos28shp" (Missing operator before cmos28shp?) String found where operator expected at line 12, near "cmo +s28shp"\n"" syntax error at line 12, near ""libName = "cmos28shp" BEGIN not safe after errors--compilation aborted at line 1 +9.
So I hope if you can show me some direction and suggest me how can I fix this. Thanks

Replies are listed 'Best First'.
Re: Creating a file by using perl
by hdb (Monsignor) on Mar 28, 2013 at 10:57 UTC

    You need to put \ in front of quotes if you want them printed.

    print FH "libName = \"cmos28shp\"\n";
      My preferred method is to use heredoc style so there is only one print statement and your quotes can remain "natural"....
      my $outfile = ""; open FH, ">> $outfile"; print FH <<EOF; libName = "cmos28shp" tech_lib = "cmos28shp" EOF close FH;

      N.B. Note that using ">>" to open skillfile appends data to it, so running the program twice will duplicate the data in the file.

      A Monk aims to give answers to those who have none, and to learn from those who know more.

        Thanks a lot man, It's working fine. Just one more thing. Now, when I am printing out those lines, I need to use values from my array. I have a subroutine in which I am parsing values from a text file and the values are stored in an array, so can I just call a subroutine and use array reference to print out my text with the values from that array ? Because when I am doing that It's giving me this error

        Global symbol "$one_ref" requires explicit package name at + line 12.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025935]
Approved by marto
[Discipulus]: ah ah I have two post in weekkly best! free coffe for all!

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2018-02-22 09:17 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (289 votes). Check out past polls.