Perl-Embed YouTube video-in Form while in edit mode

Howdy! I have a program to take notes while watching online video lectures and it’s a hassle alt+tabbing between the two windows all the time. The program for taking notes is written in Perl. It has a form to save a new notes page to a file and then a form to edit those notes. I successfully added an input field to save the embed code from the You Tube lecture and save it to a variable named $vidlect but I cant get the video to show calling it from the variable in which the embed code is stored. I added all references to Video Lecture and $vidlect to the file.
There are two modes. One to add a new note file and one to edit.
Here is the Perl code and that creates HTML form to “add” a new notes page file.

sub ActionAdd {

#  Extract info if available.
  my ( $author, $body, $kwords, $vidlect, $mimetype, $title ) = ( '', '', '', '', '', '' );
  my ( $caption ) = ('');
  my ( $isentry, $valid, $warn) = (1, 1, 0);

  if ( "$main::in{'reset'}" ne '' ) {
    $main::method = 'GET';
  }
  else {
    $author   = "$main::in{'author'}"; $author =~ s/"/"/g;
    $kwords   = lc( "$main::in{'keywords'}" ); $kwords =~ s/"/"/g;

    if ( $main::up{'upfile'}{'size'} > 0 ) { # uploaded a file

      if ("$main::in{'mimetype'}" eq "Auto-Detect") { # use browser supplied type if available
        $mimetype = "$main::up{'upfile'}{'mime'}";
        # if browser doesn't know what it is (i.e. gives octet-stream, then
        # report and error asking user to explicitly select a type
        if ("$mimetype" eq "application/octet-stream") {
	  $valid = 0;
        }
      }
      else {  # not Auto-Detect
        # use user selected type
        $mimetype = "$main::mimetypes{$main::in{'mimetype'}}";

	if ("$main::up{'upfile'}{'mime'}" ne "application/octet-stream") { # if browser has an opinion
	  if ("$mimetype" ne "$main::up{'upfile'}{'mime'}") { # that doesn't agree with the user
            $warn = 1; # just let the user know, in case they made a mistake
	  }
	}

      }
    }
    else { # an entry in the body text area, not a file upload

      if ("$main::in{'mimetype'}" ne "Auto-Detect") {
	# use user selected type
        $mimetype = "$main::mimetypes{$main::in{'mimetype'}}";
      }
      else { # Auto-Detect of text in body area
	# assume text unless it contains some typical HTML strings
	$mimetype = "text/plain";
        if ( &isHTML($main::in{'body'}) == 1) {
	  $mimetype = "text/html";
	}
      }

    }

    $isentry  = ( "$mimetype" =~ m;^text/(html|plain)$; );
    $title    = "$main::in{'title'}"; $title =~ s/"/"/g; $title =~ s/\
/ /g;
	$vidlect    = "$main::in{'vidlect'}"; $title =~ s/"/"/g; $title =~ s/\
/ /g;
    $caption  = "$main::in{'caption'}";
    if ( $isentry ) {
      $body = "$main::in{'body'}"; $body =~ s/\\r/ /g; $body =~ s/\\0//; #"
    }
    $body = $main::up{'upfile'}{'data'} if ( $main::up{'upfile'}{'size'} > 0 );
  }
########################################
# TODO NEW ENTRY FORM-EXTRACT THIS FORM AND MAKE IT EXTERNAL
########################################

#  GET:  we have either a new form, or a cancelled form.
#  In either case, put up a fresh form.
  if ( "$main::method" eq 'GET' ) {

   print <<EOF;
Content-type: text/html

<html>
<head>
<title>Add a Notebook Entry</title>
</head>
<body background="$main::backgrnd">
$main::linehorz

<h2>Add a Notebook Entry to a New Page</h2>
<p>
This form allows the user to choose from two different 
input methods. <br>
<b>Text/HTML input,</b> or <b>File/Image  upload</b>.
</p>
Fill out <b>Name</b> and <b>Title</b> (required).

EOF
  }
###### BUILD AND SAVE NEW ENTRY TO FILE
#  POST: we have a submitted form, either a notebook entry or an object.
#  RUN DEBUG AS It might be incomplete.
  elsif ( $author && $body && $mimetype && $title && $vidlect && ($valid == 1) ) {	# Complete, save it

    if ( ! $isentry ) {				# Notebook object
      my $date = time;
      my $strdate = gmtime( $date );
      $date = substr( $date, 0, length( $date ) - 3 ) . '.' .
	      substr( $date, -3 );
      my $filename = "$main::$date";
      open( FILE, ">$filename" ) || &InvalidFile();
      binmode( FILE );
      print FILE $body;
      close( FILE );
      chmod 0666, ( $filename );
      my $objs = 0;
      open( IDX, "<$main::idxfile" ) || &InvalidFile();
      flock( IDX, $main::LOCK_EX ) if ( $main::flock );
      seek( IDX, 0, 0 );
      while ( <IDX> ) {
	next if ( m/^\\s*$/ );			# Blank lines
	next if ( m/^\\s*#/ );			# Comment lines
	my ( $tag ) = split;
	$objs++ if ( $tag < 0 );
      }
      $objs++;
      flock( IDX, $main::LOCK_UN ) if ( $main::flock );
      close( IDX );
      open( IDX, ">>$main::idxfile" ) || &InvalidFile();
      flock( IDX, $main::LOCK_EX ) if ( $main::flock );
      print IDX "-$objs $date $mimetype 0\
";
      flock( IDX, $main::LOCK_UN ) if ( $main::flock );
      close( IDX );
      sleep( 1 );


      # debugging output
      if ($debugvars == 1) {
#TODO     
	 print <<EOF;
<h1>Add debug</h1>
mimetype=$mimetype<br>
main::up{'upfile'}{'mime'}=$main::up{'upfile'}{'mime'}
main::in{'body'}=$main::in{'body'}
<br><br>
EOF
      }

      if ( "$mimetype" =~ m;^image/(jpeg|pjpeg|gif|png|x-png|bmp)$; ) {
#TODO
	$body = <<EOF;
<p>
<center>
<img src="$main::relscript?nb=$main::nb&action=view&page=-$objs">
<br><b> $caption </b>
</center>
</p>
EOF
      }
      else {
	$body = <<EOF;
<p>
Click <a href="$main::relscript?nb=$main::nb&action=view&page=-$objs">here</a>
to view the object ($caption) you uploaded.
</p>
EOF
      }
      $mimetype = 'text/html';
    }

# Notebook entry
#  First parse for special SHOWTAGS regions
    $body = &parseForXML( $body );

#  Make new filename from the time.
    my $date = time;
    my $strdate = gmtime( $date );
    $date = substr( $date, 0, length( $date ) - 3 ) . '.' .
	    substr( $date, -3 );
    my $filename = "$main::root/$date";
    open( FILE, ">$filename" ) || &InvalidFile();
#TODO ADD TO PAGE HEAD
    print FILE <<EOF;

<!-- Author: $author -->
<!-- Date: $strdate (GMT) -->
<!-- Title: $title -->
<!-- Keywords: $kwords -->
<!-- Lecture: $vidlect -->
<!-- Changes: Added $strdate (GMT) $author | -->
$body
EOF
    close( FILE );
    chmod 0666, ( $filename );

#  Open the index file and count number of entries.
    my $pages = 0;
    open( IDX, "<$main::idxfile" ) || &InvalidFile();
    flock( IDX, $main::LOCK_EX ) if ( $main::flock );
    seek( IDX, 0, 0 );
    while ( <IDX> ) {
      next if ( m/^\\s*$/ );			# Blank lines
      next if ( m/^\\s*#/ );			# Comment lines
      my ( $tag ) = split;
      $pages++ if ( $tag > 0 );
    }
    $pages++;
    flock( IDX, $main::LOCK_UN ) if ( $main::flock );
    close( IDX );

#  Add new file to the index file.
    open( IDX, ">>$main::idxfile" ) || &InvalidFile();
    flock( IDX, $main::LOCK_EX ) if ( $main::flock );
    print IDX "$pages $date $mimetype 0\
";
    flock( IDX, $main::LOCK_UN ) if ( $main::flock );
    close( IDX );
 print <<EOF;
<form action="$main::script" enctype="multipart/form-data" method=post>
<p>
<pre>
<b>Your name</b>: <input type=text name=author value="$author"
 size=40>
<b>The title</b>: <input type=text name=title value="$title"
 size=40>
<b>Keywords</b> : <input type=text name=keywords value="$kwords"
 size=40> (optional)
</pre>
<b>Video Lecture</b> : <input type=text name=vidlect value="$vidlect"
 size=140> (optional)
</p>
<p>
You can <b>type</b> or <b>cut and paste</b> text or HTML 
into the text area (it will scroll to accommodate the input):<br>
<textarea rows=75 cols=175 WRAP=SOFT name=body>$body</textarea><br>
</p>
<p>
<b>Or</b> you can <b>upload a file</b> (eg. an image) to a new page:
<input type=file name=upfile value="" size=20>
<br><input type=text name=caption value="$caption" size=40>
short file description or image caption (optional)
</p>
<p>
<b>Choose the MIME type</b> of your input:
<SELECT NAME="mimetype">
EOF

  if ($main::autodetecttype == 1) {
   print "<OPTION SELECTED> Auto-Detect</OPTION>\
";
  }

  foreach ( sort keys %main::mimetypes ) {
    print '<OPTION';
    print ' SELECTED' if ( ( "$_" eq "$main::def_type" ) && ($main::autodetecttype == 0) );
    print "> $_</OPTION>\
";
  }

#TODO
  print <<EOF;
</SELECT>
</p>
<p>
Then submit your entry:<br>
<input type=submit name=submit value="Add this entry">
<input type=submit name=reset value="Start over">
<input type=submit name=cancel value="Cancel">
<input type=hidden name=action value="add">
<input type=hidden name=nb value="$main::nb">
<input type=hidden name=page value="$main::page">
</p>
</form>
</p>
</body>
</html>
EOF
  return;
}

Here is the code for the edit mode. This is where I want the video to play as it is in edit mode that I need to view the video.

sub ActionEdit {

#  Extract info if available.

  if ( "$main::in{'reset'}" ne '' ) {
    print <<EOF;
Location: $main::script?nb=$main::nb&action=view&page=$main::page

EOF
    return;
  }

  my ( $author, $body, $changes, $kwords, $mimetype, $title, $vidlect ) =
     ( '', '', '', '', '', '', '' );
  my ( $base, $filename, $notarized, $update ) = ( '', '', 0, 0 );
  my ( $pagetype ) = ( '' );
  my ( $caption  ) = ( '' );


  open( IDX, "<$main::idxfile" ) || &InvalidFile();
  flock( IDX, $main::LOCK_EX ) if ( $main::flock );
  seek( IDX, 0, 0 );
  my @lines = <IDX>;
  foreach ( @lines ) {
    next if ( m/^\\s*$/ );			# Blank lines
    next if ( m/^\\s*#/ );			# Comment lines
    my ( $tag, $name, $type, $as_is ) = split;
    if ( "$tag" eq "$main::page" ) {
      $base = "$name"; $mimetype = "$type"; $notarized = $as_is; last;
    }
  }
  flock( IDX, $main::LOCK_UN ) if ( $main::flock );
  close( IDX );

  my ( $valid, $warn) = (1, 0);
  my $oldmimetype = lc( $mimetype );
  $mimetype = lc( $mimetype );
  $filename = "$main::root/$base";

  if ( ("$main::method" eq 'GET') && ($notarized == 0)) {
    open( FILE, "<$filename" ) || &InvalidFile();
    while ( <FILE> ) {
      if    ( m/^<!-- Author: (.*) -->$/ )   { $author = $1; }
      elsif ( m/^<!-- Date: (.*) -->$/ )     { ; }
      elsif ( m/^<!-- Title: (.*) -->$/ )    { $title = $1; }
      elsif ( m/^<!-- Keywords: (.*) -->$/ ) { $kwords = $1; }
	  elsif ( m/^<!-- Video Lecture: (.*) -->$/ ) { $vidlect = $1; }
      elsif ( m/^<!-- Changes: (.*) -->$/ )  { $changes = $1; }
      else { $body .= $_; }
    }
    close( FILE );

    print <<EOF;
Content-type: text/html

<html>
<head>
<title>Edit a Notebook Entry</title>
</head>
<body background="$main::backgrnd">
$main::linehorz

<h2>Edit the Notebook Entry on This Page</h2>

<p>
This form allows the you to choose from two different
methods to modify this page. <br>
You can change or add too the existing <b>Text/HTML,</b><br>
or you can <b>upload a File/Image</b> to append to the end of the page.
<p>
The <b>Name</b> and <b>Title</b> are required.

EOF
  } elsif ( ("$main::method" eq 'GET') && ($notarized == 1)) {
    print <<EOF;
Content-type: text/html

<html>
<head>
<title>Edit invalid</title>
</head>
<body background="$main::backgrnd">
$main::linehorz

<h2>Notarized Page Can Not be Edited</h2>
<p>
The requested page has been previously notarized.<br>
Notarized pages can not be edited, but they can be annotated.
</body>
</html>
EOF
  }


#  POST: we have a submitted form, either a notebook entry or an object.
#  It might be incomplete.
  else {
    $author   = "$main::in{'author'}"; $author =~ s/"/&quot;/g;
    $changes  = "$main::in{'changes'}"; $changes =~ s/"/&quot;/g;
    $kwords   = lc( "$main::in{'keywords'}" ); $kwords =~ s/"/&quot;/g;
	$vidlect   = "$main::in{'vidlect'}" ; $vidlect =~ s/"/&quot;/g;
    $title    = "$main::in{'title'}"; $title =~ s/"/&quot;/g; $title =~ s/\
/ /g;
    $caption  = "$main::in{'caption'}";
    $body     = "$main::in{'body'}"; $body =~ s/\\r/ /g; $body =~ s/\\0//;

    if ( $main::up{'upfile'}{'size'} > 0 ) { # uploaded a file

      if ("$main::in{'mimetype'}" eq "Auto-Detect") { # use browser supplied type if available
        $mimetype = "$main::up{'upfile'}{'mime'}";
        # if browser doesn't know what it is (i.e. gives octet-stream, then
        # report and error asking user to explicitly select a type
        if ("$mimetype" eq "application/octet-stream") {
	  $valid = 0;
        }
      }
      else {  # not Auto-Detect
        # use user selected type
        $mimetype = "$main::mimetypes{$main::in{'mimetype'}}";

        if ("$main::up{'upfile'}{'mime'}" ne "application/octet-stream") { # if browser has an opinion
	  if ("$mimetype" ne "$main::up{'upfile'}{'mime'}") { # that doesn't agree with the user
	    $warn = 1; # just let the user know, in case they made a mistake
	  }
        }

      }
    }
    else { # an entry in the body text area only, no file upload

      if ("$main::in{'mimetype'}" ne "Auto-Detect") {
        # use user selected type
        $mimetype = "$main::mimetypes{$main::in{'mimetype'}}";
      }
      else { # Auto-Detect of text in body area
	# assume text unless it contains some typical HTML strings
	$mimetype = "text/plain";
        if ( &isHTML($main::in{'body'}) == 1) {
	  $mimetype = "text/html";
        }
      }
    }

    $update = ( "$oldmimetype" ne "$mimetype" );


    if (( "$mimetype" =~ m;^text/(html|plain)$; ) &&
    ( $main::up{'upfile'}{'size'} > 0 )) {
      $body .= $main::up{'upfile'}{'data'};
    }

    if (( "$mimetype" !~ m;^text/(html|plain)$; ) &&
	( $main::up{'upfile'}{'size'} > 0 )) {
      my $date = time;
      my $strdate = gmtime( $date );
      $date = substr( $date, 0, length( $date ) - 3 ) . '.' .
	      substr( $date, -3 );
      my $filename = "$main::root/$date";
      open( FILE, ">$filename" ) || &InvalidFile();
      binmode( FILE );
      print FILE $main::up{'upfile'}{'data'};
      close( FILE );
      chmod 0666, ( $filename );
      my $objs = 0;
      open( IDX, "<$main::idxfile" ) || &InvalidFile();
      flock( IDX, $main::LOCK_EX ) if ( $main::flock );
      seek( IDX, 0, 0 );
      while ( <IDX> ) {
	next if ( m/^\\s*$/ );			# Blank lines
	next if ( m/^\\s*#/ );			# Comment lines
      my ( $tag, $name, $type ) = split;
           if ( "$tag" eq "$main::page" ) {
           $base = "$name"; $pagetype = $type;
           }
	$objs++ if ( $tag < 0 );            # Count objects
      }
      $objs++;
      flock( IDX, $main::LOCK_UN ) if ( $main::flock );
      close( IDX );
      open( IDX, ">>$main::idxfile" ) || &InvalidFile();
      flock( IDX, $main::LOCK_EX ) if ( $main::flock );
      print IDX "-$objs $date $mimetype 0\
";
      flock( IDX, $main::LOCK_UN ) if ( $main::flock );
      close( IDX );
      sleep( 1 );
# Check if editor upload of Nob. If so get body and changes.
      if ( $main::in{'editor'} ne '') {
         open( IDX, "<$main::idxfile" ) || &InvalidFile();
         flock( IDX, $main::LOCK_EX ) if ( $main::flock );
         seek( IDX, 0, 0 );
         my @lines = <IDX>;
         foreach ( @lines ) {
         next if ( m/^\\s*$/ );			# Blank lines
         next if ( m/^\\s*#/ );			# Comment lines
         my ( $tag, $name, $type ) = split;
           if ( "$tag" eq "$main::page" ) {
           $base = "$name"; last;
           }
         }
         flock( IDX, $main::LOCK_UN ) if ( $main::flock );
         close( IDX );

         $filename = "$main::root/$base";

         open( FILE, "<$filename" ) || &InvalidFile();
         while ( <FILE> ) {
         if ( m/^<!-- Keywords: (.*) -->$/ ) { $kwords = $1; }
         elsif ( m/^<!-- Changes: (.*) -->$/ )  { $changes = $1; }
         else { $body .= $_; }
         }
         close( FILE );
      } 
      if ( "$mimetype" =~ m;^image/(jpeg|pjpeg|gif|png|x-png|bmp)$; ) {
	$body .= <<EOF;
<p>
<center>
<img src="$main::relscript?nb=$main::nb&action=view&page=-$objs">
<br><b> $caption </b>
</center>
</p>
EOF
      }
      else {
	$body .= <<EOF;
<p>
Click <a href="$main::relscript?nb=$main::nb&action=view&page=-$objs">here</a>
to view the object ($caption) you uploaded.
</p>
EOF
      }
      $mimetype = $pagetype;
    }

    if ( $author && $body && $mimetype && $title ) {	# Complete, save it
#  First parse and replace special SHOWTAGS regions
    $body = &parseForXML( $body );

#  Write the new file out.
      my $strdate = gmtime( time );
      open( FILE, ">$filename" ) || &InvalidFile();
      print FILE <<EOF;
<!-- Author: $author -->
<!-- Date: $strdate (GMT) -->
<!-- Title: $title -->
<!-- Keywords: $kwords -->
<!-- Video Lecture: $vidlect -->
<!-- Changes: Modified $strdate (GMT) $author | $changes -->
$body
EOF
      close( FILE );
      chmod 0666, ( $filename );

#  If the MIME type changed, update the index file.
      if ( $update ) {
        open( IDX, "<$main::idxfile" ) || &InvalidFile();
        flock( IDX, $main::LOCK_EX ) if ( $main::flock );
        seek( IDX, 0, 0 );
        my @lines = <IDX>;
        flock( IDX, $main::LOCK_UN ) if ( $main::flock );
        close( IDX );
        open( IDX, ">$main::idxfile" ) || &InvalidFile();
        flock( IDX, $main::LOCK_EX ) if ( $main::flock );
        seek( IDX, 0, 0 );
        foreach ( @lines ) {
          next if ( m/^\\s*$/ );			# Blank lines
          next if ( m/^\\s*#/ );			# Comment lines
          my ( $tag, $name, $type, $as_is ) = split;
          print IDX ( "$tag" eq "$main::page" ) ?
	    "$tag $base $mimetype $as_is\
" : "$_";
        }
        flock( IDX, $main::LOCK_UN ) if ( $main::flock );
        close( IDX );
      }

      if ($warn == 1) {
        print <<EOF;
Content-type: text/html

<html>
<head>
<title>Add a Notebook Entry</title>
</head>
<body background="$main::backgrnd">
$main::linehorz

<h2>Updated.</h2>
<p><i><b>Warning</b>: the type you selected corresponds to a MIME-type of $main::mimetypes{$main::in{'mimetype'}},
however the browser detected $main::up{'upfile'}{'mime'}.  This may or may not
have been what you intended.</i></p>
<p><a href="$main::script?nb=$main::nb&action=view&page=$main::page">
Click here to display your updated entry</a></p>
EOF
        exit $main::RET_NO_ERROR;
      }
      else {
#  Redirect the browser to the new entry.
        print <<EOF;
Location: $main::script?nb=$main::nb&action=view&page=$main::page

EOF
        exit $main::RET_NO_ERROR;
      }
    } #end if complete


    if ($valid == 0) {
      print <<EOF;
Content-type: text/html

<html>
<head>
<title>File type not detected</title>
</head>
<body background="$main::backgrnd">
$main::linehorz

<h2>Your browser was unable to determine the type of the input file.</h2>
<p>Please select a type explicitly from the options avaliable (rather than Auto-Detect).</p>
<small><i>(If a suitable type is not available from the choices provided,
ask your notebook administrator to add one to the list of MIME-types 
in the enote.pl file)</i></small>

EOF
    } 
    else {
# Incomplete entry, retry
      print <<EOF;
Content-type: text/html

<html>
<head>
<title>Incomplete Entry</title>
</head>
<body background="$main::backgrnd">
$main::linehorz

<h2>Incomplete Entry</h2>

Every notebook entry must have an author, a title, and some text.<br>
Please complete the missing parts.

EOF
    }
  } # end POST

#  Set the default MIME type.
  if ( "$mimetype" ne '' ) {
    foreach ( sort keys %main::mimetypes ) {
      $main::def_type = "$_" if ( "$main::mimetypes{$_}" eq "$mimetype" );
    }
  }

#  The rest of the 'Edit' form.
  if ($notarized == 0 ) {

print <<EOF;
<div>$vidlect</div>
EOF
  print <<EOF;
  
<form action="$main::script" enctype="multipart/form-data" method=post>
<p>
<pre>
<b>Your name</b>: <input type=text name=author value="$author"
 size=40 />
<b>The title</b>: <input type=text name=title value="$title"
 size=40 />
<b>Keywords</b> : <input type=text name=keywords value="$kwords"
 size=40 /> (optional)
 
 <b>Video Lecture</b> : <input type=text name=vidlect value="$vidlect"
 size=140 />

</pre>
</p>

<p>
You can <b>type</b> or <b>cut and paste</b> into the text area (it will
scroll to accommodate the input):<br>
<textarea rows=15 cols=175 WRAP=SOFT name=body>$body</textarea><br>
</p>

<p>
Or you can <b>upload a file</b> (eg. an image) to the end of this page: 
<input type=file name=upfile value="" size=20>
<br><input type=text name=caption value="$caption" size=40>
short file description or image caption (optional)
<br>The image will be centered and displayed with the specified caption.
</p>
<p>
<b>Choose the MIME type</b> of your input:
<SELECT NAME="mimetype">
EOF

  if ($main::autodetecttype == 1) {
    print "<OPTION SELECTED> Auto-Detect</OPTION>\
";
  }

  foreach ( sort keys %main::mimetypes ) {
    print '<OPTION';
    print ' SELECTED' if ( ( "$_" eq "$main::def_type" ) && ($main::autodetecttype == 0) );
    print "> $_</OPTION>\
";
  }

  print <<EOF;
</SELECT>
</p>
<p>
Then submit your entry:<br>
<input type=submit name=submit value="Update this entry">
<input type=submit name=reset value="Start over">
<input type=submit name=cancel value="Cancel">
<input type=hidden name=action value="edit">
<input type=hidden name=nb value="$main::nb">
<input type=hidden name=page value="$main::page">
<input type=hidden name=changes value="$changes">
</p>
</form>

</p>
</body>
</html>
EOF
  }
  return;
}

I apologize if that is too much code. I just didn’t want to leave any thing relevant out.
You can find the block I added that is actually capturing the link to the video but not displaying it in the browser if you search and find the code below.
If I do "view page source I can see it in the source but again no video display.

print <<EOF;
<div>$vidlect</div>
EOF
  print <<EOF;

This also worked for displaying in the source but not in the browswer.

<div>$vidlect</div>

Any help would be appreciated.
Thanks in advance.

Update: Tsalagi received more help at PerlMonks, as seen in the thread [url=http://perlmonks.org/?node_id=844217]Displaying Youtube Video.

I resolved this issue.