Newbie File Question

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default Re: Newbie File Question

    Kubaton Lover wrote at Fri, 22 Aug 2003 14:17:51 +0000:
    > I have a perl script that currently opens a file and prints its contents to
    > the web browser like so...
    >
    > open (CODE, $file) || die;
    > while (<CODE>) {
    > print $_;
    > }
    > close CODE;
    >
    >
    > I need to change the code to read and output just the first line, then I'll
    > output some custom stuff, then I need to output the rest of the file;
    > something like the following pseudocode:
    >
    > open (CODE, $file) || die;
    > print first line;
    > print my stuff here;
    > print rest of file;
    > close CODE;
    >
    > Your help is appreciated.
    E.g.

    open CODE, $file or die $!; # I would also add the error code
    print scalar <CODE>; # print first line
    print $your_stuff;
    while (<CODE>) {
    print; # the $_ is redundant here
    }
    close CODE;


    Greetings,
    Janek
    Janek Schleicher Guest

  2. Similar Questions and Discussions

    1. newbie question: getting an embedded file from a plugin
      I have a pdf document, and I added a attachment. How do I programmatically get that file from a plug ins? I can see an embeddedfile field in the...
    2. newbie question: getting an embedded file
      I have a pdf document, and I added a attachment. How do I programmatically get that file from a plug ins? I can see an embeddedfile field in the...
    3. Newbie question - how to call one swf file from another at end
      I see plenty of examples of how to do this on a button press, and I'm sure this is even more simple, but I'm so green I can't see it yet. I've...
    4. newbie question: creating a new record in a related file
      somaBoyMX wrote: You can do it with a Portal in the Contacts file. First, the relationship from the Contacts file as Master to the related...
  3. #2

    Default Re: Newbie File Question

    Janek Schleicher <bigj@kamelfreund.de> wrote in comp.lang.perl.misc:
    > Kubaton Lover wrote at Fri, 22 Aug 2003 14:17:51 +0000:
    [[[.]
    > > I need to change the code to read and output just the first line, then I'll
    > > output some custom stuff, then I need to output the rest of the file;
    > > something like the following pseudocode:
    > >
    > > open (CODE, $file) || die;
    > > print first line;
    > > print my stuff here;
    > > print rest of file;
    > > close CODE;
    > >
    > > Your help is appreciated.
    >
    > E.g.
    >
    > open CODE, $file or die $!; # I would also add the error code
    > print scalar <CODE>; # print first line
    To make it watertight add a check for eof():

    print scalar <CODE> unless eof( CODE);

    Otherwise it will try to print an undefined value if the input file
    is empty.
    > print $your_stuff;
    > while (<CODE>) {
    > print; # the $_ is redundant here
    > }
    > close CODE;
    Anno
    Anno Siegel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139